package com.cwb.study.springbootstudy.config;

import com.cwb.study.springbootstudy.component.LoginHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ：chenwenbin
 * @date ：Created in 2019/08/08 10:03
 * @description：用于MVC路径映射和控制器注册
 */
//@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //注册路径
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //注册路径,重定向到index
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/index").setViewName("login");
        registry.addViewController("/main").setViewName("main");
        registry.addViewController("/404").setViewName("404");
        registry.addViewController("/demo").setViewName("demo");
        registry.addViewController("/user/list").setViewName("user/list");


    }
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有请求,但是放行登录页和登陆方法请求
        //需要手动放行: webjars
        //静态资源: *.css,*.js 会自动放行

        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/","/index.html","/user/login","/webjars/**");

    }
}
